<template>
    <BPTable ref='tableRef' align='center' row-key='id' name-key='name' :columns='columns' :api='pageApi' :buttons='buttons'>

    </BPTable>
    <BDialog v-model:visible='visible' :title='dialogTitle' width='500px' :confirm='submit' @close='close'>
        <a-form ref='formRef' :model='formData' :label-col="{ span: 5 }">
            <a-form-item style='display:none' name='id'>
                <a-input type='hidden' v-model:value='formData.id' />
            </a-form-item>
            <a-form-item :label="$t('name')" name='name' label-align="left" :rules="[{required : true}]">
                <a-input :maxlength='30' show-count type='text' v-model:value='formData.name' />
            </a-form-item>
            <a-form-item label="电价" name='price' label-align="left" :rules="[{required : true}]">
                <a-input :min="0" type='number' v-model:value.number='formData.price'>
                    <template #addonAfter>元/度</template>
                </a-input>
            </a-form-item>
            <a-form-item label="服务费" name='service' label-align="left" :rules="[{required : true}]">
                <a-input :min="0" type='number' v-model:value.number='formData.service'>
                    <template #addonAfter>元/度</template>
                </a-input>
            </a-form-item>
            <a-form-item label="每小时电量" name='hourKwh' label-align="left" :rules="[{required : true}]">
                <a-input :min="0" type='number' v-model:value.number='formData.hourKwh'>
                    <template #addonAfter>度</template>
                </a-input>
            </a-form-item>
        </a-form>
    </BDialog>
</template>

<script lang='ts' setup>
    import { nextTick, ref } from "vue";
    import { message, Modal } from "ant-design-vue";

    
    import { pageApi, saveApi, deleteApi } from "@/api/pricingRule/Slow";
    import { parseTime } from "@/utils";
    import { useResourceStore } from "@/store/resource";
    import { useI18nStore } from "@/store/i18n";

    const i18n = useI18nStore()
    const resourceStore = useResourceStore()

    const visible = ref(false)
    const formRef = ref()
    const dialogTitle = ref()
    const tableRef = ref()


    const buttons = ref<SearchButton[]>([])

    if(resourceStore.hasPermission('pricing:add')) {
        buttons.value.push({
            button : 'add',
            func : () => {
                
                dialogTitle.value = i18n.t('add')
                visible.value = true
            }
        })
    }


    const formData = ref<any>({"id":0,"name":"","price":'',"service":'',"hourKwh":''})



    //关闭的时候清空表单
    function close() {
        formRef.value.resetFields()
        formRef.value.clearValidate()
    }

    

    async function submit() {
        let params  = await formRef.value.validate()
        params.price = Number(params.price)
        params.service = Number(params.service)
        params.hourKwh = Number(params.hourKwh)
        
        const res = await saveApi(params)
        if(res.success) {
            tableRef.value.reload()
            message.success(i18n.t('saveSuccess'))
            close()
            visible.value = false
        }else {
            Modal.error({
                title : res.msg
            })
        }
    }




    const columns = ref<Column[]>([
        {
            dataIndex : 'name',
            title : i18n.t('name'),
            inSearch : true,
        },

    
        {
            dataIndex : 'price',
            title : '电价（元/度）',
        },

        {
            dataIndex : 'service',
            title : '服务费（元/度）',
        },

        {
            dataIndex : 'hourKwh',
            title : '每小时电量（度）',
        },

        {
            title : i18n.t('updateTime'),
            dataIndex : 'updateTime',
            align : 'center',
            customRender({text}) {
                return parseTime(text)
            }
        },

        {
            title : i18n.t('createTime'),
            dataIndex : 'createTime',
            align : 'center',
            customRender({text}) {
                return parseTime(text)
            }
        },
        
        {
            dataIndex : '$action',
            title : i18n.t('operation'),
            align : 'center',
            $edit : resourceStore.hasPermission('pricing:update') ? (row) => {
                dialogTitle.value = i18n.t('edit')
                visible.value = true
                nextTick(() => {
                    for(let k in formData.value) {
                        if(k in row) {
                            formData.value[k] = row[k]
                        }
                    }
                })
            } : undefined,

            $delete : resourceStore.hasPermission('pricing:delete') ? async(row, index) => {
                const res = await deleteApi(row.id)
                if(res.success) {
                    tableRef.value.reload()
                    message.success(i18n.t('deleteSuccess'))
                }else {
                    message.error(res.msg)
                }
            } : undefined
            
        },
    ])



</script>

<style lang="scss" module>
    .ruleRow{
        display: flex;
        align-items: center;

        .input{
            flex: 1;
        }
        .minus{
            margin-left: 10px;
            color: #999;
            cursor: pointer;
            font-size: 24px;
        }
    }

    .ruleItem{
        margin-bottom: 0 !important;
    }
</style>